package a01_数组;

/**
 * <p>
 * a04_有序数组的平方复习6
 * </p>
 *
 * @author flyduck
 * @since 2025/4/7
 */
public class a04_有序数组的平方复习6 {
    public int[] sortedSquares(int[] nums) {
        int[] result = new int[nums.length];
        int resultIdx = result.length - 1;

        int leftIdx = 0;
        int rightIdx = nums.length - 1;
        while (leftIdx <= rightIdx) {
            if(nums[leftIdx] * nums[leftIdx] >= nums[rightIdx] * nums[rightIdx]){
                result[resultIdx] = nums[leftIdx] * nums[leftIdx];
                leftIdx++;
            }else {
                result[resultIdx] = nums[rightIdx] * nums[rightIdx];
                rightIdx--;
            }
            resultIdx--;
        }
        return result;
    }
}
